/**
  * Created by Alex on 2016/7/23.
  */
import scala.collection.mutable
import scala.io._

object Driver {
  case class UserStep(val userid:Int,val time:Int,val counter:Int){}
  def main(args:Array[String]): Unit ={

//    val path = "D:\\aasdataset\\audioscrobbler\\user_artist_data.txt"
//    val rawUserArtistData = Source.fromFile(path).getLines()
//    rawUserArtistData.map(_.split(" ")(0).toDouble).take(5).foreach(println)
//    print("hello world")
    def merge(map:mutable.HashMap[Int,Int], us:UserStep)={
        map += (us.userid->(map.getOrElse(us.userid,0)+us.counter))
        map
}
    def combo(map1:mutable.HashMap[Int,Int],map2:mutable.HashMap[Int,Int])  ={
      for((k,v)<-map2){
        map1 += ( k ->(map1.getOrElse(k,0)+v))
      }
      map1
    }
    val list = List(UserStep(1,1,10),UserStep(1,1,10), UserStep(1,2,20),UserStep(1,3,30),UserStep(2,3,40),UserStep(2,4,50))
    var map = new mutable.HashMap[Int,Int]()
    map = list.aggregate(map)(merge,combo)
//    for((k,v)<-map){
//      println(k,v)
//    }
    list.map(us=>(us.userid,us.counter)).groupBy(f=>f._1).mapValues(list=>list.reduce((x,y)=>(x._1,x._2+y._2))).foreach(println)

//    list.map(us=>(us.userid,us.counter)).
  }


}
